package com.charonmc.commands;

import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

public class Fly implements CommandExecutor {
    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        if (!sender.hasPermission("Core.Fly")) {
            return true;
        } else {
            //无参命令
            if (args.length == 0) {
                //筛选一波命令发送者
                if (!(sender instanceof Player)) {
                    sender.sendMessage("§c§l只有玩家可以无参执行！");
                } else {
                    Player player = (Player) sender;
                    if (player.getAllowFlight()) {
                        sender.sendMessage("§b§l您的飞行模式已设置为§c§l关闭。");
                    } else {
                        sender.sendMessage("§b§l您的飞行模式已设置为§a§l开启。");
                    }
                    setPlayerFlyMode(player);
                }
            } else if (args.length == 1) {
                //筛选一波玩家在不在线
                if (Bukkit.getPlayer(args[0]) == null) {
                        sender.sendMessage("§c§l所指玩家不在线！");

                } else {
                    Player player = Bukkit.getPlayer(args[0]);
                    if (player.getAllowFlight()) {
                        setPlayerFlyMode(player);
                        sender.sendMessage("§b§l该玩家飞行模式已设置为§c§l关闭。");
                    } else {
                        sender.sendMessage("§b§l该玩家飞行模式已设置为§a§l开启。");
                    }
                }
            } else {
                    sender.sendMessage("§c§l参数错误！");
            }
        }
        return true;
    }
    private static void setPlayerFlyMode(Player player) {
        player.setAllowFlight(!player.getAllowFlight());
    }
}

